<?php
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\module\Classes\wiki\SpuToWiki;
use Biz\model\wiki\SpuRelateWiki;
$spuId = isset($argv[1]) ? $argv[1] : 0;
$where = ['wiki_id[>]' => 0, 'relate_status' => 1];
if ($spuId > 0) {
    $where['goods_id'] = $spuId;
}
$rs = SpuRelateWiki::select(['wiki_id', 'goods_id'], $where);
if ($rs) {
    $wikiIds = array_column($rs, 'wiki_id');
    $list = \Biz\model\wiki\Wiki::selectByWhere(['id' => $wikiIds]);
    $list = empty($list) ? [] : array_column($list, null, 'id');

    //todo
    foreach ($rs as $v) {
        $wikiInfo = isset($list[$v['wiki_id']]) ? $list[$v['wiki_id']] : [];
        $goodsId = $v['goods_id'];
        if ($wikiInfo && $goodsId > 0) {
            SpuToWiki::toRelateWiki($goodsId, $wikiInfo);
            echo "spu: $goodsId 转换任务已经添加\n";
        } else {
            echo "spu: $goodsId 的wiki信息不存在\n";
        }
    }
} else {
    echo 'no data';
}


return false;
$shopId = isset($argv[1]) ? $argv[1] : 0;
$status = isset($argv[2]) ? $argv[2] : SpuRelateWiki::STATUS_WAIT;
$close = isset($argv[3]) ? $argv[3] : 0;


$where = [];
if ($shopId > 0) {
    $where['shop_id'] = $shopId;
}
if ($status >= 0) {
    $where['relate_status'] = $status;
}
if (empty($where)) {
    echo '无参数';
    return;
}
$where['relate_status[!]'] = SpuRelateWiki::STATUS_RELATE_SUCCESS;
$rs = SpuRelateWiki::select('*', $where);
if (!empty($rs)) {
    $shopMap = [];
    foreach ($rs as $v) {
        $shopMap[$v['shop_id']][$v['goods_id']] = $v;
    }
    foreach ($shopMap as $shop_id => $v) {
        $rs = SpuToWiki::wikiReUpload($v, $shop_id);
        echo "shop_id:$shop_id 执行" . count($v);
    }
} else {
    echo 'no data';
}


//todo 修改已经删除的商品转wiki
if ($close) {
    \Biz\model\wiki\Wiki::updateData(['status' => \Biz\model\wiki\Wiki::STATUS_DEL], ['id' => [6976, 6999, 7015, 7022, 7023, 7024, 7025, 7026, 7027, 7062, 7079]]);
}


